package com.example.mymvvmdemo.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;


import org.jetbrains.annotations.NotNull;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public abstract class BaseVBAdapter<T, V extends ViewBinding> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    protected List<T> dataList; //数据
    protected Context context; //上下文

    /**
     * @param context
     */
    public BaseVBAdapter(Context context) {
        this.context = context;
        dataList = new ArrayList<>();
    }

    /**
     * 添加数据
     *
     * @param dataList 数据
     */
    public void setDataList(List<T> dataList) {
        if (dataList == null) {
            this.dataList = new ArrayList<>();
        } else {
            this.dataList = dataList;
        }
        notifyDataSetChanged();
    }

    /**
     * 添加更多的数据
     *
     * @param dataList 数据
     */
    public void addDataList(List<T> dataList) {
        if (dataList == null) {
            dataList = new ArrayList<>();
        }
        this.dataList.addAll(dataList);
        notifyItemRangeChanged(this.dataList.size() - dataList.size(), this.dataList.size());
    }


    /**
     * 布局绑定
     *
     * @param parent   父视图
     * @param viewType 视图类型
     */
    @NotNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        if (viewType == -1) {
            View view = new View(context);
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    needEmptyHeight());
            view.setLayoutParams(lp);
            view.setBackgroundResource(needEmptyColor());
            return new EmptyHolder(view);
        }
        V bind = getReflectVB(parent);
        if (bind == null) {
            return new CustomizeHolder(new View(context));
        }
        return new CustomizeHolder(bind);
    }

    /**
     * 数据更新
     */
    @Override
    public void onBindViewHolder(@NonNull @NotNull RecyclerView.ViewHolder viewHolder, int position) {
        if (needEmptyView() && viewHolder instanceof EmptyHolder) {
            //空视图不需要赋值
            return;
        }
        CustomizeHolder holder = (CustomizeHolder) viewHolder;
        if (dataList.size() == 0) {
            return;
        }
        T item = dataList.get(position);
        V bind = holder.bind;
        if (bind == null) {
            return;
        }
        updateData(bind, item, position);
    }

    /**
     * 更新holder数据
     *
     * @param bind     布局
     * @param item     数据
     * @param position 位置
     */
    protected abstract void updateData(V bind, T item, int position);


    /**
     * 获取适配器布局bind
     *
     * @param parent 父布局
     * @return 通过反射获取ViewBinding
     */
    private V getReflectVB(ViewGroup parent) {
        Type type = this.getClass().getGenericSuperclass();
        if (type != null) {
            Class<V> vbClass = (Class<V>) ((ParameterizedType) type).getActualTypeArguments()[1];
            try {
                Method method = vbClass.getDeclaredMethod("inflate", LayoutInflater.class, ViewGroup.class, boolean.class);
                return (V) method.invoke(null, LayoutInflater.from(context), isNeedParent() ? parent : null, false);
            } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
                return null;
            }
        }
        return null;
    }

    protected boolean isNeedParent() {
        return true;
    }

    /**
     * 是否需要空占位视图
     *
     * @return 默认false
     */
    protected boolean needEmptyView() {
        return false;
    }

    /**
     * 空视图高度
     *
     * @return 默认200
     */
    protected int needEmptyHeight() {
        return 200;
    }

    /**
     * 空视图颜色
     *
     * @return 默认透明
     */
    protected int needEmptyColor() {
        return android.R.color.transparent;
    }

    /**
     * 获取适配器数组
     *
     * @return List<T>
     */
    public List<T> getDataList() {
        return dataList;
    }

    /**
     * 获取适配器数组长度
     *
     * @return List<T>
     */
    public int getDataListSize() {
        if (dataList == null)
            return 0;
        return dataList.size();
    }

    /**
     * 如果需要空视图数组长度加1
     *
     * @return 数组长度
     */
    @Override
    public int getItemCount() {
        if (needEmptyView()) {
            return dataList == null ? 0 : dataList.size() + 1;
        }
        return dataList == null ? 0 : dataList.size();
    }

    /**
     * 判断是否有空视图
     *
     * @param position 数据位置
     * @return -1代表有空视图
     */
    @Override
    public int getItemViewType(int position) {
        if (position == dataList.size() && needEmptyView()) {
            return -1;
        }
        return super.getItemViewType(position);
    }

    /**
     * 数据holder
     */
    public class CustomizeHolder extends RecyclerView.ViewHolder {
        private V bind;

        CustomizeHolder(V bind) {
            super(bind.getRoot());
            this.bind = bind;
        }

        CustomizeHolder(View view) {
            super(view);
        }
    }

    /**
     * 空视图holder
     */
    public static class EmptyHolder extends RecyclerView.ViewHolder {

        public EmptyHolder(View view) {
            super(view);
        }
    }
}